// IST 6353 - Project 1
// The functions in this file will help you will construct the path cities
// as well as populate the destinations cities

var NUMBER_OF_PATH_CITIES = 5;
var NUMBER_OF_DESTINATIONS = 4;
var HOME_CITY = "San Antonio, USA";
var NON_PATH_CITY_CLUE = "No clues here.";
var DEFAULT_FOUND = "Lady Deedi was found and Arrested";

function GameCities()
{
	this.aCities = new Array();
	this.aPathCities = new Array();
	this.aNonPathCities = new Array();
	this.lock = false;
}

// This function adds three strings in this order: city name, file name, and  
// a clue 
GameCities.prototype.addCity = function( cityName, fileName, clueText ) 
{

	this.aCities.push( { city: cityName, file: fileName, clue: clueText } );
};


// This function takes the first NUMBER_OF_PATH_CITIES from the list
// and makes those the path cities.  Also, set the non path cities.
// For true randomization in a game the programmer should have randomized the list first.
GameCities.prototype.setPathCities = function ()
{
	var i;
	for( i = 0; i < NUMBER_OF_PATH_CITIES; i = i + 1 )
	{
		this.aPathCities[ i ] = this.aCities[ i ];
	}
	// set the non path cities
	for( i = NUMBER_OF_PATH_CITIES; i < this.aCities.length; i = i + 1 )
	{
		this.aNonPathCities[ i - NUMBER_OF_PATH_CITIES ] = this.aCities[ i ];
	}
};

// This function returns a list of destinations that can be traveled
// to from a given city.  Note the spelling of the city MUST be exact.
// If the city is NOT a path the city, it will take the three four 
// cities on the list as destination cities.
GameCities.prototype.getDestinations = function( departureCity )
{
	var pathCityIndex = this.getPathCityIndex( departureCity );
	var aDestinations = new Array();
	var i;
	
	// If the departure city is the home city, take the first path city 
	// and then 3 non path cities
	if( departureCity == HOME_CITY )
	{
		// add the first path city
		aDestinations.push( { city: this.aPathCities[ 0 ].city, file: this.aPathCities[ 0 ].file, clue: this.aPathCities[ 0 ].clue } );
		
		// not a path city, so take the next city as destinations with wrap around
		for( i = 0; i < NUMBER_OF_DESTINATIONS - 1; i = i + 1 )
		{
			aDestinations.push( { city: this.aNonPathCities[ i ].city, file: this.aNonPathCities[ i ].file, clue: this.aNonPathCities[ i ].clue } );
			
		}
	}
	// If the departure city is not the home city...
	else
	{
		// if on the path
		if( pathCityIndex != -1 )
		{
			for( i = 0; i < NUMBER_OF_DESTINATIONS - 1; i = i + 1 )
			{
				var newIndex = ( ( pathCityIndex + 2 * NUMBER_OF_DESTINATIONS + i + 1 ) % this.aNonPathCities.length );
				aDestinations.push( { city: this.aNonPathCities[ newIndex ].city, file: this.aNonPathCities[ newIndex ].file, clue: this.aNonPathCities[ newIndex ].clue } );
			}
			
			// if it's the last path city, add the next non path city
			if( pathCityIndex == this.aPathCities.length - 1 )
			{
				var newIndex = ( ( pathCityIndex + 2 * NUMBER_OF_DESTINATIONS + i + 1 ) % this.aNonPathCities.length );
			aDestinations.push( { city: this.aNonPathCities[ newIndex ].city, file: this.aNonPathCities[ newIndex ].file, clue: this.aNonPathCities[ newIndex ].clue } );
			}			
			else
			{
				// else, add the next path city
				aDestinations.push( { city: this.aPathCities[ pathCityIndex + 1 ].city, file: this.aPathCities[ pathCityIndex + 1 ].file, clue: this.aPathCities[ pathCityIndex + 1 ].clue } );
			}
		}
		else
		{
			var index = this.getIndex( departureCity );
			// not a path city, so take the next city as destinations with wrap around
			for( i = 0; i < NUMBER_OF_DESTINATIONS; i = i + 1 )
			{
				var newIndex = ( ( index + i + 1 ) % this.aNonPathCities.length );
				aDestinations.push( { city: this.aCities[ newIndex ].city, file: this.aCities[ newIndex ].file, clue: this.aCities[ newIndex ].clue } );
			}
		}
	}
	this.randomizeArray( aDestinations );
	
	return aDestinations;
};

// This function returns the clue for the next city given a departureCity
GameCities.prototype.getClue = function( departureCity )
{
	// if the departure is the home city, then return the first
	// path cty's clue
	if( departureCity == HOME_CITY )
	{
		return this.aPathCities[ 0 ].clue;
	}

	var clue = NON_PATH_CITY_CLUE;
	
	var index = this.getPathCityIndex( departureCity );
	
	if( index != -1 )
	{
	
		// the departure city is the final city which means the
		// criminal was found
		if( index == NUMBER_OF_PATH_CITIES - 1 )
		{	
			clue = DEFAULT_FOUND;
		}
		else 
		{
			// otherwise send the next city's clue
			clue = this.aPathCities[ index + 1 ].clue;
		}
	}
	
	return clue;
}

// This function returns the index of the depatureCity in the 
// path city list.  Note the spelling of the city MUST be exact.
GameCities.prototype.getPathCityIndex = function( departureCity )
{
	for( i = 0; i < this.aPathCities.length; i = i + 1 )
	{
		if( departureCity == this.aPathCities[ i ].city )
		{
			return i;
		}
	}
	// At this point, it was not found, so return -1
	return -1;
};


// This function returns the index of the depatureCity in the 
// list.  Note the spelling of the city MUST be exact.
GameCities.prototype.getIndex = function( departureCity )
{
	for( i = 0; i < this.aCities.length; i = i + 1 )
	{
		if( departureCity == this.aCities[ i ].city )
		{
			return i;
		}
	}
	// At this point, it was not found, so return -1
	return -1;
};


// This function returns the index of the depatureCity in the 
// index.  Note the spelling of the city MUST be exact.
GameCities.prototype.getIndex = function( departureCity )
{
	for( i = 0; i < this.aCities.length; i = i + 1 )
	{
		if( departureCity == this.aCities[ i ].city )
		{
			return i;
		}
	}
	// At this point, it was not found, so return -1
	return -1;
};

// This function display an alert with all the cities
GameCities.prototype.displayCities = function()
{
	var message = "";
	var i;
	for( i = 0; i < this.aCities.length; i = i + 1 )
	{
		message = message + this.aCities[ i ].city + "\n";	
	}
	alert( message );
};


// This function display an alert with all the path cities
GameCities.prototype.displayPathCities = function()
{
	var message = "";
	var i;
	for( i = 0; i < NUMBER_OF_PATH_CITIES; i = i + 1 )
	{
		message = message + this.aPathCities[ i ].city + "\n";	
	}
	alert( message );
};


// This function display an alert with all the cities in an array
GameCities.prototype.displayArray = function( array )
{
	var message = "";
	var i;
	for( i = 0; i < array.length; i = i + 1 )
	{
		message = message + array[ i ].city + "\n";	
	}
	alert( message );
};

// This function randomizes the cities on the list.  This function should
// only be called once!!!
GameCities.prototype.randomizeCities = function()
{
	// This code is from 
	// http://www.hardcode.nl/subcategory_1/article_317-array-shuffle-function
	var len = this.aCities.length;
	var i = len;
	while ( i-- ) 
	{
		var p = parseInt( Math.random() * len );
		var t = this.aCities[ i ];
		this.aCities[ i ] = this.aCities[ p ];
		this.aCities[ p ] = t;
	}
};

// This function randomizes any array.  The randomized array is returned 
// through the parameter
GameCities.prototype.randomizeArray = function( array )
{
	// This code is from 
	// http://www.hardcode.nl/subcategory_1/article_317-array-shuffle-function
	var len = array.length;
	var i = len;
	while ( i-- ) 
	{
		var p = parseInt( Math.random() * len );
		var t = array[ i ];
		array[ i ] = array[ p ];
		array[ p ] = t;
	}
};

